<template>
    <el-dialog :title="isAddRole ? '新增角色' : '修改角色'" :close-on-click-modal="false" :visible.sync="visible" center>
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="dataForm.roleName" placeholder="角色名称"></el-input>
            </el-form-item>
            <el-form-item  label="所属工作台" prop="workbenchCode">
                <el-input :disabled="!isAddRole" v-model="dataForm.workbenchCode" placeholder="所属工作台"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            isAddRole: true,
            dataForm: {
                id: null,
                roleName: "",
                workbenchCode:""
            },
            dataRule: {
                roleName: [
                    { required: true, message: '角色名称不能为空', trigger: 'blur' }
                ],
                workbenchCode: [
                    { required: true, message: '所属工作台不能为空', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        init(data) {
            this.visible = true;
            this.isAddRole = true;
            this.dataForm = {}
            if (data) {
                this.dataForm = data;
                this.isAddRole = false;
            }
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.$http({
                        url: this.isAddRole ? "/pms/admin/role/addRole" : "/pms/admin/role/updateRole",
                        method: 'post',
                        data: {
                            roleId: this.dataForm.id,
                            roleName: this.dataForm.roleName,
                            workbenchCode: this.dataForm.workbenchCode
                        }
                    }).then((response) => {
                        if (response && response.code == "0") {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        } else {
                            this.$message.error(response.message)
                        }
                    })
                }
            })
        }
    }
}
</script>
